﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace WebCore.Helpers
{
    public class FileHelper
    {

        public static string UploadFileToSever(HttpPostedFileBase myFile, string uploadPath)
        {
            if (myFile == null)
                return string.Empty;

            if (myFile.ContentLength < 0 && myFile.ContentLength > 3145728) // 3 Magebytes
                return string.Empty;

            string fileName = Path.GetFileName(myFile.FileName);
            string filePath = Path.Combine(HttpContext.Current.Server.MapPath(uploadPath), fileName);
            myFile.SaveAs(filePath);

            return filePath;
        }

        public static void DeleteFile(string filePath)
        {
            if (File.Exists(filePath))
                File.Delete(filePath);
        }


        /// <summary>
        /// Get the file name from local path or URL
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetFileName(string path)
        {
            if (path.StartsWith("http"))
            {
                Uri uri = new Uri(path);
                return Path.GetFileName(uri.LocalPath);
            }
            else
            {
                return Path.GetFileName(path);
            }

        }


        /// <summary>
        /// Get the file extention from local path or URL
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetFileExtention(string path)
        {
            if (path.StartsWith("http"))
            {
                Uri uri = new Uri(path);
                return Path.GetExtension(uri.LocalPath);
            }
            else
            {
                return Path.GetExtension(path);
            }
        }

        /// <summary>
        /// Get the file name without extention from local path or URL
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetFileNameWithoutExtention(string path)
        {
            if (path.StartsWith("http"))
            {
                Uri uri = new Uri(path);
                return Path.GetFileNameWithoutExtension(uri.LocalPath);
            }
            else
            {
                return Path.GetFileNameWithoutExtension(path);
            }
        }


        /// <summary>
        /// Set the year, month, day and guidID as the file name
        /// but it does not have extenion
        /// </summary>
        /// <returns></returns>
        public static string SetTimeAsFileName(string extention)
        {
            var now = DateTime.Now;

            return string.Format("{0}-{1}-{2}-{3}{4}"
                , now.Year
                , now.Month
                , now.Day
                , Guid.NewGuid().ToString()
                , extention);
        }



    }
}
